<html>
<head>
    <script src="../../resources/js-test.js"></script>
    <script>
        description('This tests the XMLHttpRequest responseXML loading an HTML document in Quirks Mode with responseType "document".');

        window.jsTestIsAsync = true;
        var xhr = new XMLHttpRequest(),
            url = window.location.href,
            children;
        xhr.onload = function() {
            shouldBeNonNull('xhr.responseXML');
            shouldBeTrue('xhr.responseXML instanceof Document');

            debug('The two open &lt;br&gt; elements should be siblings of one another and not nested per HTML parser spec.');

            // Get the children of the body tag, using QSA to skip all text nodes.
            children = xhr.responseXML.querySelectorAll('body > *');

            // Test that the HTML parser was used by confirming the two BR tags are adjacent siblings
            // and not nested within one another, nor nesting the div#console node.
            // There should be 5 non-text children altogether.
            shouldEvaluateTo('children.length', 5);
            shouldBeEqualToString('children[0].tagName', 'DIV');
            shouldBeEqualToString('children[0].id', 'description');
            shouldBeEqualToString('children[1].tagName', 'BR');
            shouldBeEqualToString('children[2].tagName', 'BR');
            shouldBeEqualToString('children[3].tagName', 'DIV');
            shouldBeEqualToString('children[3].id', 'console');
            shouldBeEqualToString('children[4].tagName', 'SCRIPT');

            // Make sure the first BR tag doesn't have an "id" set, which will prove
            // the first script in this document was not executed.
            shouldNotBe('children[1].id', '"break-tag"');

            // The parsed HTML document in responseXML should be the same compatMode as this document.
            // This document is in quirks mode due to a missing DOCTYPE.
            shouldBeEqualToString('xhr.responseXML.compatMode', 'BackCompat');
            shouldBeEqualToString('xhr.responseXML.compatMode', document.compatMode);

            finishJSTest();
        };
        xhr.onerror = function() {
            testFailed('The XHR request to an existing resource failed: "' + url + '"');
            finishJSTest();
        };
        xhr.open('GET', url);
        xhr.responseType = 'document';
        xhr.send(null);
    </script>
</head>
<body>
    <div id="description"></div>

    <!-- Leave these break tags malformed/open to test HTML parsing.
         They should be siblings to div#description, and not nested within one another.
         The XMLDocument parser would treat the second <br> as a child to the first. -->
    <br><br>

    <div id="console"></div>

    <script>
        // This code will manipulate the first BR node by adding an "id" to it.
        // This same BR will be inspected after XHR loads the document to see
        // if this script executed or not.  If it didn't execute, the first BR
        // will not have an "id" specified and the test passes.
        var br = document.querySelector('div#description + br');
        br.id = 'break-tag';
    </script>
</body>
</html>
